package com.wnxy.consumer;

import cn.hutool.core.lang.UUID;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class test {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testDelayMessage() {
        //1.准备消息
        Message message = MessageBuilder.withBody("hello ttl message".getBytes())
                .setDeliveryMode(MessageDeliveryMode.PERSISTENT)
                .setHeader("x-delay", 5000) // 消息延迟时间
                .build();
        //2.消息 ID 需要封装到 CorrelationData 中
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        //3.发送消息
        rabbitTemplate.convertAndSend("delay.direct", "delay", message, correlationData);
        System.out.println("消息已经成功发送!");
    }
}
